package com.zl.tby.service.impl;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.model.TbyFileBusiness;
import com.zl.tby.repository.TbyFileBusinessRepo;
import com.zl.tby.repository.TbyMountainPeakRepo;
import com.zl.tby.service.ITbyFileBusinessService;
import org.noear.solon.annotation.Component;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 附件与业务关系表接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyFileBusinessServiceImpl implements ITbyFileBusinessService {

    @Db
    private TbyFileBusinessRepo tbyFileBusinessRepo;


    @Override
    public void saveAttach(String businessIdentifying, Long businessId, String attach) {
        if (StringUtils.isEmpty(attach)) {
            // 删除全部关系
            deleteByIdentifyingAndId(businessIdentifying, businessId);
            return;
        }
        List<TbyFileBusiness> attachList = getAttach(businessIdentifying, businessId);
        Map<String, Long> attachUrlToId = new HashMap<>(0);
        for (TbyFileBusiness entity : attachList) {
            attachUrlToId.put(entity.getFileUrl(), entity.getId());
        }
        String[] array = ToolsUtil.splitStr(attach, ',');
        // 保存需要新增的附件关系
        Map<String, Integer> attachMap = new HashMap<>(0);
        for (String s : array) {
            attachMap.put(s, 1);
            if (!attachUrlToId.containsKey(s)) {
                // 在数据库中不存在，需要保存关系
                TbyFileBusiness entity = new TbyFileBusiness();
                entity.setBusinessId(businessId);
                entity.setBusinessIdentifying(businessIdentifying);
                entity.setCreateBy(ToolsUtil.getLoginUserId());
                entity.setCreateTime(LocalDateTime.now());
                entity.setFileUrl(s);
                String[] path = ToolsUtil.splitStr(s, '/');
                if (path.length > 2) {
                    entity.setFileName(path[path.length - 1]);
                    entity.setBucketName(path[2]);
                    String[] fileNameArray = ToolsUtil.splitStr(path[path.length - 1], '.');
                    if (fileNameArray.length > 0) {
                        entity.setFileId(fileNameArray[0]);
                    }
                }
                this.tbyFileBusinessRepo.insert(entity, true);
            }
        }
        // 删除不在本次保存中的数据
        for (TbyFileBusiness entity : attachList) {
            if (!attachMap.containsKey(entity.getFileUrl())) {
                this.tbyFileBusinessRepo.deleteById(entity.getId());
            }
        }
    }

    @Override
    public int deleteByIdentifyingAndId(String businessIdentifying, Long businessId) {
        if (StringUtils.isEmpty(businessIdentifying) || businessId == null) {
            return 0;
        }
        return this.tbyFileBusinessRepo.delete(mp -> {
            mp.whereTrue()
                    .andEq("business_identifying", businessIdentifying)
                    .andEq("business_id", businessId);
        });
    }

    @Override
    public List<TbyFileBusiness> getAttach(String businessIdentifying, Long businessId) {
        if (StringUtils.isEmpty(businessIdentifying) || businessId == null) {
            return new ArrayList<>();
        }
        return this.tbyFileBusinessRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue()
                    .andEq("business_identifying", businessIdentifying)
                    .andEq("business_id", businessId);
        });
    }
}
